package util

import (
	"errors"
	"gateway/config"
	"go.uber.org/zap"
	"strings"
)

func ToMap(str, sep1, sep2 string) (map[string]string, error) {
	if str == "" || sep1 == "" || sep2 == "" {
		return nil, errors.New("str or sep is Empty")
	}
	m := make(map[string]string)
	strs := strings.Split(str, sep1)
	for _, it := range strs {
		if it == "" {
			continue
		}

		its := strings.Split(it, sep2)
		if len(its) != 2 {
			config.Logger.Warn("String can not split",
				zap.String("string", it),
				zap.String("separator", sep2))
			continue
		}
		k := strings.TrimSpace(its[0])
		v := strings.TrimSpace(its[1])
		m[k] = v
	}
	return m, nil
}
